<?php
/*
 * @美潮风
 * @功能说明：亿美短信封装层
 * @更新说明：暂无更新
 * @文件名 YiMei.class.php
 * @编码 UTF-8
 * @创建时间 2015-6-2 下午4:41:16
 * @创建人 XING
 */
namespace service\yimei;
require SCRIPT_ROOT.'/../../library/yimei/include/Client.php';

class YiMei {
	/*网关地址 */
	private $gwUrl = 'http://sdk4report.eucp.b2m.cn:8080/sdk/SDKService';
	/*序列号*/
	private $serialNumber = '6SDK-EMY-6688-KIUSS';
	/*密码*/
	private $password = '0000';
	/*SessionKey*/
	private $sessionKey = '123456';

	/*必须的固定参数*/
	private $connectTimeOut = 2;
	private $readTimeOut = 10;
	private $proxyhost = false;
	private $proxyport = false;
	private $proxyusername = false;
	private $proxypassword = false;

	/*
	 * 对象初始化
	 * @date: 2015-4-8下午3:07:04
	 * @editor: XING
	 */
	public function init(){
		$client =  new \Client(
					$this->gwUrl,
					$this->serialNumber,
					$this->password,
					$this->sessionKey,
					$this->proxyhost,
					$this->proxyport,
					$this->proxyusername,
					$this->proxypassword,
					$this->connectTimeOut,
					$this->readTimeOut
				);
		$client->setOutgoingEncoding("utf-8");
		return $client;
	}

	/*
	 * 内容转换
	 * @date: 2015-4-8下午3:20:21
	 * @editor: XING
	 */
	private static function iconver($content){
		$content = preg_replace('/【.+】$/','',$content);
	//	if(!preg_match('/^【.+】/', $content))$content = '【灯商云】'.$content;
		if(!preg_match('/退订回复TD$/', $content))$content = $content.'，退订回复TD';
		return $content;
	}

	/*
	 * 短信发送
	 * @date: 2015-4-8下午3:30:44
	 * @editor: XING
	 */
	public function sendsms($phones, $content){
		/*发送短信*/
		$statusCode = self::init()->sendSMS(explode(',',$phones),self::iconver($content));

		/*返回验证*/
		if ($statusCode!=null && $statusCode==0){
			/*发送成功*/

		}else{
			/*发送失败*/
		}

		return $statusCode;
	}

	/*
	 * 查询短信余额费用
	 * @date: 2015-4-8下午3:17:51
	 * @editor: XING
	 */
	public function getBlance(){
		return self::init()->getBalance();
	}
	
	
	/*
	 * Sms other service
	 * @date: 2015-6-24 上午10:55:14
	 * @editor: XING
	 */
	public function sendcode($mobile,$content){
		$content = trim($content).'【美潮风】';
		//拼装url地址
		$url = "http://sms.bechtech.cn/Api/send/data/json?accesskey=3409&secretkey=33a18748d9da8c84e542d79b8e52f5d027a7f749&mobile=".$mobile."&content=".urlencode($content);
		$json = curl_get($url);
		$arr = json_decode($json,true); //格式化返回数组
		
		return $arr['result']=='01';
	}
	
	/*
	 * 创蓝中国 SMS服务
	 * @date: 2015-8-20 下午1:25:58
	 * @editor: XING
	 */
	public function pdlSms($mobile,$msg=''){
		return curl_get('http://222.73.117.138:7891/mt?un=N18668110209&pw=287291&da='.$mobile.'&sm='.bin2hex(iconv('utf-8','gbk','您本次验证码为 '.$msg.'，10分钟内有效 【美潮风】')).'&dc=15&rd=1');
		
	/* 	$post_data = array();
		$post_data['account'] = iconv('GB2312', 'GB2312',"yunjia");//yunjia、jiekou-clcs-08
		$post_data['pswd'] = iconv('GB2312', 'GB2312',"Txb123456");//Txb123456、Tch123456
		$post_data['mobile'] = trim($mobile);
		
		$post_data['msg']=mb_convert_encoding(self::iconver($msg),'UTF-8', 'auto');
		$url='http://222.73.117.158/msg/HttpBatchSendSM?';
		$o="";
		foreach ($post_data as $k=>$v){
			$o.= "$k=".urlencode($v)."&";
		}
		$post_data=substr($o,0,-1);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

		$result = curl_exec($ch);
		print_r($result);exit; */
	}
}


?>